#include "lib.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void stash::initialize(int Size)
{
    size = Size;
    quantity = 0;
    next = 0;
    storage = 0;
}

void stash::cleanup()
{
    if (storage)
    {
	puts("Freeing storage\n");
	free(storage);
    }
}

int stash::add(void *element)
{
    if (next >= quantity)
    {
	inflate(100);
    }

    memcpy(&(storage[next * size]), element, size);
    next++;
    return (next-1);
}

void *stash::fetch(int index)
{
    if (index >= next || index < 0)
    {
	return 0;
    }

    return &(storage[index * size]);
}

int stash::count()
{
    return next;
}

void stash::inflate(int increase)
{
    void *v = realloc(storage, (quantity + increase) * size);
    assert(v);
    storage = (unsigned char *)v;
    quantity += increase;
}
